/**
 * meilele.com Inc.
 * Copyright (c) 2004-2016 All Rights Reserved.
 */
package com.gpagers.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuDetectEvent;
import org.eclipse.swt.events.MenuDetectListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
import org.quartz.SchedulerException;

import com.gpagers.core.ObjectRef;
import com.gpagers.core.ScheduleInstance;

/**
 * 
 * @version $Id: Main.java, v 0.1 2016年5月9日 上午11:19:29 liuzijie@meilele.com Exp $
 */
public class Main {
    public static void main(String[] args) {
        ObjectRef bf = new ObjectRef();
        MultiTaskGUI mutiTask = new MultiTaskGUI();
        initSystemIcon(mutiTask);
        mutiTask.getShell().setSize(800, 500);
        center();
        mutiTask.getShell().layout();
        mutiTask.getShell().open();

        while (!mutiTask.getShell().isDisposed()) {
            if (!ObjectRef.display.readAndDispatch()) {
                ObjectRef.display.sleep();
            }
        }

        try {
            ScheduleInstance.getInstance().getScheduler().shutdown();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        ObjectRef.display.dispose();
    }

    private static void initSystemIcon(final MultiTaskGUI mutiTask) {
        //构造系统栏控件
        final Tray tray = ObjectRef.display.getSystemTray();
        final TrayItem trayItem = new TrayItem(tray, SWT.NONE);

        //程序启动时，窗口是显示的，所以系统栏图标隐藏
        trayItem.setVisible(false);
        trayItem.setToolTipText(mutiTask.getShell().getText());
        ObjectRef.trayItem = trayItem;
        trayItem.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                toggleDisplay(mutiTask.getShell(), tray);
            }
        });

        final Menu trayMenu = new Menu(mutiTask.getShell(), SWT.POP_UP);
        MenuItem showMenuItem = new MenuItem(trayMenu, SWT.PUSH);
        showMenuItem.setText("显示窗口(&s)");

        //显示窗口，并隐藏系统栏中的图标
        showMenuItem.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                toggleDisplay(mutiTask.getShell(), tray);
            }
        });

        trayMenu.setDefaultItem(showMenuItem);

        new MenuItem(trayMenu, SWT.SEPARATOR);

        //系统栏中的退出菜单，程序只能通过这个菜单退出
        MenuItem exitMenuItem = new MenuItem(trayMenu, SWT.PUSH);
        exitMenuItem.setText("退出程序(&x)");

        exitMenuItem.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                mutiTask.getShell().dispose();
                try {
                    ScheduleInstance.getInstance().getScheduler().shutdown();
                } catch (SchedulerException e) {
                    e.printStackTrace();
                }
            }
        });

        //在系统栏图标点击鼠标右键时的事件，弹出系统栏菜单
        trayItem.addMenuDetectListener(new MenuDetectListener() {
            public void menuDetected(MenuDetectEvent e) {
                trayMenu.setVisible(true);
            }
        });

        trayItem.setImage(mutiTask.getShell().getImage());

        //注册窗口事件监听器
        mutiTask.getShell().addShellListener(new ShellAdapter() {

            //点击窗口最小化按钮时，窗口隐藏，系统栏显示图标
            public void shellIconified(ShellEvent e) {
                toggleDisplay(mutiTask.getShell(), tray);
            }

            //点击窗口关闭按钮时，并不终止程序，而时隐藏窗口，同时系统栏显示图标
            public void shellClosed(ShellEvent e) {
                e.doit = false; //消耗掉原本系统来处理的事件
                toggleDisplay(mutiTask.getShell(), tray);
            }
        });
    }

    /**
     * 窗口是可见状态时，则隐藏窗口，同时把系统栏中图标删除
     * 窗口是隐藏状态时，则显示窗口，并且在系统栏中显示图标
     * @param shell 窗口
     * @param tray 系统栏图标控件
     */
    private static void toggleDisplay(Shell shell, Tray tray) {
        try {
            shell.setVisible(!shell.isVisible());
            tray.getItem(0).setVisible(!shell.isVisible());
            if (shell.getVisible()) {
                shell.setMinimized(false);
                shell.setActive();
                ObjectRef.isMinAtCampo = false;
            } else {
                ObjectRef.isMinAtCampo = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void center() {
        Rectangle bounds = ObjectRef.display.getPrimaryMonitor().getBounds();
        Rectangle rect = ObjectRef.mainShell.getBounds();
        int x = bounds.x + (bounds.width - rect.width) / 2;
        int y = bounds.y + (bounds.height - rect.height) / 2;
        ObjectRef.mainShell.setLocation(x, y);
    }
}
